<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Todo:
* images inside links
* Base URL
-->
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
<xsl:output method="html" indent="yes" />

<!-- This code was derived from a post to the xsl-list mailing list by Jeni Tennison at:
http://www.biglist.com/lists/xsl-list/archives/200012/msg00175.html
It is purely genious...-->

<!--
Most likely this will change once Mozilla supports EXSLT. In particular, node-set() would
be helpful in implementing a two-pass version of this transform that should allow for much more
detailed output, including verifying h3's that have no h2 before them (for example).

However, it should be possible to do this in the existing implementation
-->


<!-- One change : chaned all preceding-sibling to preceding -->
<xsl:key name="next-headings" match="h6"
         use="generate-id(preceding::*[self::h1 or self::h2 or
                                               self::h3 or self::h4 or
                                               self::h5][1])" />
<xsl:key name="next-headings" match="h5"
         use="generate-id(preceding::*[self::h1 or self::h2 or
                                               self::h3 or self::h4][1])" />
<xsl:key name="next-headings" match="h4"
         use="generate-id(preceding::*[self::h1 or self::h2 or
                                               self::h3][1])" />
<xsl:key name="next-headings" match="h3"
         use="generate-id(preceding::*[self::h1 or self::h2][1])" />
<xsl:key name="next-headings" match="h2"
         use="generate-id(preceding::h1[1])" />

<xsl:key name="immediate-nodes"
         match="node()[not(self::h1 | self::h2 | self::h3 | self::h4 |
                           self::h5 | self::h6)]"
         use="generate-id(preceding-sibling::*[self::h1 or self::h2 or
                                               self::h3 or self::h4 or
                                               self::h5 or self::h6][1])" />

<xsl:template match="h1 | h2 | h3 | h4 | h5 | h6">
	<xsl:variable name="level" select="substring-after(name(), 'h')" />
	<li>Level <xsl:value-of select="$level" />: <b><xsl:value-of select="text()" /></b>
		<xsl:choose>
			<xsl:when test="$level=1">
				<!--<xsl:attribute name="style">border: solid black; margin: 5px</xsl:attribute>
				<h1><xsl:value-of select="text()" /></h1>-->
			</xsl:when>
			<xsl:when test="$level=2">
				<!--<xsl:attribute name="style">border: solid black; margin: 5px</xsl:attribute>
				<h2><xsl:value-of select="text()" /></h2>-->
				<xsl:if test="count(preceding::h1)=0">
					<div align="center"><b>ERROR : level 2 header with no level 1 header before it</b></div>
				</xsl:if>
			</xsl:when>
			<xsl:when test="$level=3">
				<!--<xsl:attribute name="style">border: solid black; margin: 5px</xsl:attribute>
				<h3><xsl:value-of select="text()" /></h3>-->
				<xsl:if test="count(preceding::h2)=0">
					<div align="center"><b>ERROR : level 3 header with no level 2 header before it</b></div>
				</xsl:if>
			</xsl:when>
			<xsl:when test="$level=4">
				<!--<xsl:attribute name="style">border: solid black; margin: 5px</xsl:attribute>
				<h4><xsl:value-of select="text()" /></h4>-->
				<xsl:if test="count(preceding::h3)=0">
					<div align="center"><b>ERROR : level 4 header with no level 3 header before it</b></div>
				</xsl:if>
			</xsl:when>
			<xsl:when test="$level=5">
				<!--<xsl:attribute name="style">border: solid black; margin: 5px</xsl:attribute>
				<h5><xsl:value-of select="text()" /></h5>-->
				<xsl:if test="count(preceding::h4)=0">
					<div align="center"><b>ERROR : level 5 header with no level 4 header before it</b></div>
				</xsl:if>
			</xsl:when>
			<xsl:when test="$level=6">
				<!--<xsl:attribute name="style">border: solid black; margin: 5px</xsl:attribute>
				<h6><xsl:value-of select="text()" /></h6>-->
				<xsl:if test="count(preceding::h5)=0">
					<div align="center"><b>ERROR : level 6 header with no level 5 header before it</b></div>
				</xsl:if>
			</xsl:when>
		</xsl:choose>
	<!-- If we wanted to process internal nodes, we could -->
		<!--<div style="border: solid blue; margin: 4px;"><xsl:apply-templates /></div>-->
		<!--<xsl:apply-templates select="key('immediate-nodes', generate-id())" />-->
		<ul><xsl:apply-templates select="key('next-headings', generate-id())" /></ul>
	</li>
</xsl:template>

<xsl:template match="/html/body">
<html>
<body>
	<h1>Outline View</h1>
	<p>This view provides an outline of how the level headers in the provided document may appear to an accessibility tool.
		Headers have been converted into a hiearchy based on their level. Note that some warnings may be given if there is a
		header of say level 6 that does not follow after a level 5 header.</p>
	<hr />
	<xsl:if test="count(h1) =0">
		<h2>Error : no level 1 header. In order to generate a valid outline, you need a level 1 header.</h2>
		<!-- This is actually a true statement, because the above ID generating code will not work if there is no
		starting level 1 header. There MAY be a way to fix this at some point -->
	</xsl:if>
	<xsl:if test="count(h1)>0">
		<ul>
			<xsl:apply-templates select="h1" />
		</ul>
	</xsl:if>
</body>
</html>
</xsl:template>

<!--
<xsl:template match="node()">
   <xsl:copy-of select="." />
</xsl:template>
-->
</xsl:stylesheet>